package com.bst.bsj.bops.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.SeedlingModelParamsService;
import com.bst.bsj.common.dal.dataobject.Merchant;
import com.bst.bsj.common.dal.dataobject.SeedlingModelParams;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.StringUtil;

@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/sModelParams")
public class SeedlingModelParamsController extends WebBaseController {

  @Autowired
  private SeedlingModelParamsService seedlingModelParamsService;

  /**
   * 
  * @Title: auditList
  * @Description: TODO(这里用一句话描述这个方法的作用)
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月14日 上午11:41:37
  * @modificator :tonglu
  * @modify date :2016年5月14日 上午11:41:37
   */
  @SuppressWarnings("unchecked")
  @RequestMapping(value = "/sModelParamsList")
  public ModelAndView auditList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    ModelAndView modelAndView = getBopsModelAndView("goods/parameter");
    String name = (String) params.get("name");//名称
    if (name != null) {
      name = name.trim();
    }
    String pageNums = (String) params.get("pageNum");
    Integer pageNum = 0;
    if (StringUtil.isNotBlank(name)) {
      paramsMap.put("name", name);
    }
    if (StringUtil.isNumeric(pageNums)) {
      pageNum = Integer.valueOf(pageNums);
    }
    if (pageNum == null || pageNum == 0) {
      pageNum = 1;
    }
    paramsMap.put("page", pageNum);
    paramsMap.put("length", new Long(10));
    paramsMap.put("offset", new Long((pageNum - 1) * 10));
    Result result = seedlingModelParamsService.query(paramsMap);
    // 如果请求成功
    if (result.isSuccess()) {
      // 得到分页器
      Paginator paginator = (Paginator) result.getModels().get("paginator");
      // 得到数据
      List<SeedlingModelParams> seedlingModelParams = (List<SeedlingModelParams>) result.getModels().get(
          "seedlingModelParams");
      modelAndView.addObject("seedlingModelParams", seedlingModelParams);
      modelAndView.addObject("paginator", paginator);
    }
    modelAndView.addObject("params", params);
    return modelAndView;
  }

  /**
   * 
  * @Title: edit
  * @Description: TODO(编辑)
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 下午7:34:50
  * @modificator :tonglu
  * @modify date :2016年5月13日 下午7:34:50
   */
  @RequestMapping(value = "/edit")
  public ModelAndView edit(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    ModelAndView modelAndView = new ModelAndView();
    SeedlingModelParams seedlingModelParams = seedlingModelParamsService.getSeedlingModelParams(Long.valueOf(id));
    modelAndView.addObject("seedlingModelParams", seedlingModelParams);
    return modelAndView;
  }

  @RequestMapping(value = "/save")
  @ResponseBody
  public String save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    SeedlingModelParams seedlingModelParams = new SeedlingModelParams();
    String id = (String) params.get("id");
    if (id != null) {
      seedlingModelParams = seedlingModelParamsService.getSeedlingModelParams(Long.valueOf(id));
    }
    String name = (String) params.get("name");
    String unit = (String) params.get("unit");
    seedlingModelParams.setName(name);
    seedlingModelParams.setUnit(unit);

    if (id != null) {
      Boolean exsitName = getExsit(name, Long.valueOf(id));
      if (!exsitName) {
        return null;
      }
      seedlingModelParamsService.update(seedlingModelParams);
    } else {
      Boolean exsitName = getExsit(name, null);
      if (!exsitName) {
        return null;
      }
      seedlingModelParamsService.insert(seedlingModelParams);
    }
    return "success";
  }

  @RequestMapping(value = "/delete")
  public void delete(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    seedlingModelParamsService.delete(Long.valueOf(id));
  }

  @RequestMapping(value = "/getParam")
  @ResponseBody
  public List<SeedlingModelParams> getParam(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    List<SeedlingModelParams> list = seedlingModelParamsService.getAll();
    return list;
  }

  /**
  * @Title: getExsit
  * @Description:判断参数名唯一
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * Boolean
  * @throws 
  * @creator     :yj
  * @create date :2016年5月21日 下午7:19:26
  * @modificator :yj
  * @modify date :2016年5月21日 下午7:19:26
   */
  @RequestMapping(value = "/getExsit")
  @ResponseBody
  public Boolean getExsit(String name, Long id) throws Exception {
    if (StringUtil.isNotBlank(name)) {
      SeedlingModelParams modelParam = seedlingModelParamsService.getExsit(name);
      if (modelParam == null) {
        return true;
      } else {
        if (id != null) {
          Long mid = modelParam.getId();
          if (mid.equals(id)) {
            return true;
          }
        }
      }
    }
    return false;
  }

}
